package cn.itcast.n3_Java线程;

import lombok.extern.slf4j.Slf4j;

import java.io.IOException;

/**
 * @program: juc
 * @description:
 * @author: flyduck
 * @create: 2024-08-29 17:44
 **/
@Slf4j(topic = "c.Test26_线程的六种状态_演示六种状态")
public class Test26_线程的六种状态_演示六种状态 {
    public static void main(String[] args) throws IOException {

        Thread t1 = new Thread("t1") {//new
            @Override
            public void run() {
                log.debug("running...");
            }
        };

        Thread t2 = new Thread("t2") {
            @Override
            public void run() {
                while(true) { // runnable

                }
            }
        };
        t2.start();

        Thread t3 = new Thread("t3") {//terminated
            @Override
            public void run() {
                log.debug("running...");
            }
        };
        t3.start();

        Thread t4 = new Thread("t4") {
            @Override
            public void run() {
                synchronized (Test26_线程的六种状态_演示六种状态.class) {
                    try {
                        Thread.sleep(1000000); // timed_waiting
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };
        t4.start();

        Thread t5 = new Thread("t5") {
            @Override
            public void run() {
                try {
                    t2.join(); // waiting
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
        t5.start();

        Thread t6 = new Thread("t6") {
            @Override
            public void run() {
                synchronized (Test26_线程的六种状态_演示六种状态.class) { // blocked
                    try {
                        Thread.sleep(1000000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        };
        t6.start();

        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        log.debug("t1 state {}", t1.getState().toString().toLowerCase());//new
        log.debug("t2 state {}", t2.getState().toString().toLowerCase());//runnable
        log.debug("t3 state {}", t3.getState().toString().toLowerCase());//terminated
        log.debug("t4 state {}", t4.getState().toString().toLowerCase());//timed_waiting
        log.debug("t5 state {}", t5.getState().toString().toLowerCase());//waiting
        log.debug("t6 state {}", t6.getState().toString().toLowerCase());//blocked
        System.in.read();
    }
}
